<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Create a RecursiveFilterIterator from a RecursiveIterator</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="class.recursivefilteriterator.html">RecursiveFilterIterator</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="recursivefilteriterator.getchildren.html">RecursiveFilterIterator::getChildren</a></div>
 <div class="up"><a href="class.recursivefilteriterator.html">RecursiveFilterIterator</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="recursivefilteriterator.construct" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">RecursiveFilterIterator::__construct</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.1.0)</p><p class="refpurpose"><span class="refname">RecursiveFilterIterator::__construct</span> &mdash; <span class="dc-title">Create a RecursiveFilterIterator from a RecursiveIterator</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-recursivefilteriterator.construct-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="methodname"><strong>RecursiveFilterIterator::__construct</strong></span>
    ( <span class="methodparam"><span class="type"><a href="class.recursiveiterator.html" class="type RecursiveIterator">RecursiveIterator</a></span> <code class="parameter">$iterator</code></span>
   )</div>

  <p class="para rdfs-comment">
   Create a <a href="class.recursivefilteriterator.html" class="classname">RecursiveFilterIterator</a> from a <a href="class.recursiveiterator.html" class="classname">RecursiveIterator</a>.
  </p>

 </div>


 <div class="refsect1 parameters" id="refsect1-recursivefilteriterator.construct-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<em><code class="parameter">iterator</code></em></dt>

     <dd>

      <p class="para">
       The <a href="class.recursiveiterator.html" class="classname">RecursiveIterator</a> to be filtered.
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-recursivefilteriterator.construct-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   没有返回值。
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-recursivefilteriterator.construct-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-3802">
    <p><strong>Example #1 Basic <span class="methodname"><strong>RecursiveFilterIterator()</strong></span> example</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">TestsOnlyFilter&nbsp;</span><span style="color: #007700">extends&nbsp;</span><span style="color: #0000BB">RecursiveFilterIterator&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">accept</span><span style="color: #007700">()&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;Accept&nbsp;the&nbsp;current&nbsp;item&nbsp;if&nbsp;we&nbsp;can&nbsp;recurse&nbsp;into&nbsp;it<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;or&nbsp;it&nbsp;is&nbsp;a&nbsp;value&nbsp;starting&nbsp;with&nbsp;"test"<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">return&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">hasChildren</span><span style="color: #007700">()&nbsp;||&nbsp;(</span><span style="color: #0000BB">strpos</span><span style="color: #007700">(</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">current</span><span style="color: #007700">(),&nbsp;</span><span style="color: #DD0000">"test"</span><span style="color: #007700">)&nbsp;!==&nbsp;</span><span style="color: #0000BB">FALSE</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br /></span><span style="color: #0000BB">$array&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">"test1"</span><span style="color: #007700">,&nbsp;array(</span><span style="color: #DD0000">"taste2"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"test3"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"test4"</span><span style="color: #007700">),&nbsp;</span><span style="color: #DD0000">"test5"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$iterator&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">RecursiveArrayIterator</span><span style="color: #007700">(</span><span style="color: #0000BB">$array</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$filter&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">TestsOnlyFilter</span><span style="color: #007700">(</span><span style="color: #0000BB">$iterator</span><span style="color: #007700">);<br /><br />foreach(new&nbsp;</span><span style="color: #0000BB">RecursiveIteratorIterator</span><span style="color: #007700">(</span><span style="color: #0000BB">$filter</span><span style="color: #007700">)&nbsp;as&nbsp;</span><span style="color: #0000BB">$key&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$value</span><span style="color: #007700">)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">$value&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程的输出类似于：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
test1
test3
test4
test5
</pre></div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="example-3803">
    <p><strong>Example #2 <span class="methodname"><strong>RecursiveFilterIterator()</strong></span> example</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">StartsWithFilter&nbsp;</span><span style="color: #007700">extends&nbsp;</span><span style="color: #0000BB">RecursiveFilterIterator&nbsp;</span><span style="color: #007700">{<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;protected&nbsp;</span><span style="color: #0000BB">$word</span><span style="color: #007700">;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">__construct</span><span style="color: #007700">(</span><span style="color: #0000BB">RecursiveIterator&nbsp;$rit</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$word</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">word&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$word</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">parent</span><span style="color: #007700">::</span><span style="color: #0000BB">__construct</span><span style="color: #007700">(</span><span style="color: #0000BB">$rit</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">accept</span><span style="color: #007700">()&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">hasChildren</span><span style="color: #007700">()&nbsp;OR&nbsp;</span><span style="color: #0000BB">strpos</span><span style="color: #007700">(</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">current</span><span style="color: #007700">(),&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">word</span><span style="color: #007700">)&nbsp;===&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">getChildren</span><span style="color: #007700">()&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;new&nbsp;</span><span style="color: #0000BB">self</span><span style="color: #007700">(</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getInnerIterator</span><span style="color: #007700">()-&gt;</span><span style="color: #0000BB">getChildren</span><span style="color: #007700">(),&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">word</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br /></span><span style="color: #0000BB">$array&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">"test1"</span><span style="color: #007700">,&nbsp;array(</span><span style="color: #DD0000">"taste2"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"test3"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"test4"</span><span style="color: #007700">),&nbsp;</span><span style="color: #DD0000">"test5"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$iterator&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">RecursiveArrayIterator</span><span style="color: #007700">(</span><span style="color: #0000BB">$array</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$filter&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">StartsWithFilter</span><span style="color: #007700">(</span><span style="color: #0000BB">$iterator</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"test"</span><span style="color: #007700">);<br /><br />foreach(new&nbsp;</span><span style="color: #0000BB">RecursiveIteratorIterator</span><span style="color: #007700">(</span><span style="color: #0000BB">$filter</span><span style="color: #007700">)&nbsp;as&nbsp;</span><span style="color: #0000BB">$key&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$value</span><span style="color: #007700">)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">$value&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程的输出类似于：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
test1
test3
test4
test5
</pre></div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-recursivefilteriterator.construct-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="methodname"><a href="recursivefilteriterator.getchildren.html" class="methodname" rel="rdfs-seeAlso">RecursiveFilterIterator::getChildren()</a> - Return the inner iterator's children contained in a RecursiveFilterIterator</span></li>
    <li class="member"><span class="methodname"><a href="recursivefilteriterator.haschildren.html" class="methodname" rel="rdfs-seeAlso">RecursiveFilterIterator::hasChildren()</a> - Check whether the inner iterator's current element has children</span></li>
    <li class="member"><span class="methodname"><a href="filteriterator.accept.html" class="methodname" rel="rdfs-seeAlso">FilterIterator::accept()</a> - Check whether the current element of the iterator is acceptable</span></li>
   </ul>
  </p>
 </div>


</div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="class.recursivefilteriterator.html">RecursiveFilterIterator</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="recursivefilteriterator.getchildren.html">RecursiveFilterIterator::getChildren</a></div>
 <div class="up"><a href="class.recursivefilteriterator.html">RecursiveFilterIterator</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
